//
//  XZStudentPayVC.m
//  优学荟管理系统
//
//  Created by edz on 2018/1/11.
//  Copyright © 2018年 星道三好. All rights reserved.
//

#import "XZStudentPayVC.h"
#import "XZStudentPayCell.h"
#import "XZStudentNoPayCell.h"

static NSString *payCell = @"XZStudentPayCell";
static NSString *noPayCell = @"XZStudentNoPayCell";

@interface XZStudentPayVC ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isTrue;
@property (nonatomic, strong) NSMutableArray <XZStudentdetailModel *>*dataSources;

@end

@implementation XZStudentPayVC

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self setupUI];
    [self setUpRefresh];
    _dataSources = [NSMutableArray array];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTargat:self action:@selector(Pop) image:@"icon_back_student_sign" highImage:@""];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(255,255,255)}]; //设置标题颜色
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(255, 255, 255)] forBarMetrics:(UIBarMetricsDefault)];
    
    [SVProgressHUD dismiss];
}


- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(255, 255, 255)] forBarMetrics:(UIBarMetricsDefault)];//设置导航栏颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(51,51,51)}];//设置标题颜色
}


- (void)setupUI {
    
    self.title = self.isFlang.length != 0 ? @"缴费学生" : @"未缴费学生";
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = self.isFlang.length != 0 ? 160 : 135;
    _tableView.tableFooterView = [UIView new];
    _tableView.backgroundColor = RGBColor(245, 245, 245);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    if (self.isFlang.length != 0) {
          [_tableView registerNib:[UINib nibWithNibName:@"XZStudentPayCell" bundle:nil] forCellReuseIdentifier:payCell];
    } else {
           [_tableView registerNib:[UINib nibWithNibName:@"XZStudentNoPayCell" bundle:nil] forCellReuseIdentifier:noPayCell];
    }
    
  
    [self.view addSubview:_tableView];
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.contentInset = UIEdgeInsetsMake(-54, 0, 0, 0);
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark --- 上拉加载和下拉刷新方法的实现
- (void)setUpRefresh{
    
    //下拉刷新
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    //自动更改透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    //结束头部刷新
    [self.tableView.mj_header endRefreshing];
    //结束尾部刷新
    [self.tableView.mj_footer endRefreshing];
    
    
}

/**
 *  上拉加载
 */
- (void)upRefresh{
    
    self.isTrue = NO;
    self.page++;
    [self setNetWork];
    
}
/**
 *  下拉刷新
 */

- (void)downRefresh{
    self.isTrue = YES;
    self.page = 1;
    [self setNetWork];
}

- (void)setNetWork{
    // http://192.168.0.226:8080/StarShine/ys/trainLesson.do?action=selStudentByOrderId&oid=1&orderId=&pageNumber=&startTime=&endTime=
    NSString *url = [NSString stringWithFormat:@"trainLesson.do?action=selStudentByOrderId&oid=%@&orderId=%@&pageNumber=%ld&startTime=%@&endTime=%@",[AccountTool shareAccountTool].currentAccount.oid,_isFlang.length != 0 ? @"1" : @"0",(long)_page,self.dateArr.firstObject,self.dateArr.lastObject];
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.oid);
    
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        [SVProgressHUD dismiss];
        if ([[Obj valueForKey:@"code"]integerValue] == 1000) {
            if (self.isTrue == YES) {
                [self.dataSources removeAllObjects];
            }
            NSDictionary *dict = [Obj valueForKey:@"t"];
            NSArray *arr = [dict valueForKey:@"dataList"];
            if (arr != nil && ![arr isKindOfClass:[NSNull class]] && arr.count != 0){
                for (NSDictionary *dic in arr) {
                    XZStudentdetailModel *model = [XZStudentdetailModel new];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.dataSources addObject:model];
                }
            }
            
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }
        
    } Fail:^(NSError *Error) {
        
        [SVProgressHUD dismiss];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSources.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (self.isFlang.length != 0) {
        XZStudentPayCell *cell = [tableView dequeueReusableCellWithIdentifier:payCell forIndexPath:indexPath];
        cell.model = _dataSources[indexPath.row];
        return cell;
    } else {
        XZStudentNoPayCell *cell = [tableView dequeueReusableCellWithIdentifier:noPayCell forIndexPath:indexPath];
        cell.model = _dataSources[indexPath.row];
        cell.CallPhone = ^(NSString *phoneNum) {
            [self phoneChatNum:phoneNum];
        };
        return cell;
    }
}

- (void)phoneChatNum:(NSString *)phoneNum{
    NSString *strs = phoneNum;
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",strs];
    UIWebView *callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}

- (void)Pop {
    
    if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]) {
        //判断1
        [self dismissViewControllerAnimated:YES completion:nil];
    } else if ([self.navigationController respondsToSelector:@selector(popViewControllerAnimated:)]) {
        //判断2
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
